package com.cakebackoffice.Model;


import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import com.cakebackoffice.database.DataSource;
import com.cakebackoffice.database.IDatabasable;
import com.cakebackoffice.database.ProductTable;
import com.cakebackoffice.database.StoreTable;

import java.util.ArrayList;
import java.util.List;


public class Store implements IDatabasable {
    private Long idDB;
    private Long remoteID;
    private String name;
    private Float estimatedServiceTime = 0f;
    private Place place;
//    private Dictionary<ProductType, LocalPriceHT> availableTypeProducts = new Hashtable<ProductType, LocalPriceHT>();
//    private Dictionary<MenuType, LocalPriceHT> availableMenuType = new Hashtable<MenuType, LocalPriceHT>();


    private Store(DataSource dataSource, Cursor cursor) {

        idDB = cursor.getLong(cursor.getColumnIndexOrThrow(StoreTable.ID));
        setRemoteID(cursor.getLong(cursor.getColumnIndexOrThrow(ProductTable.REMOTE_ID)));
        name = cursor.getString(cursor.getColumnIndexOrThrow(StoreTable.NAME));
        estimatedServiceTime = cursor.getFloat(cursor.getColumnIndexOrThrow(StoreTable.ESTIMATEDSERVICETIME));
        place = Place.getOne(dataSource, cursor.getInt(cursor.getColumnIndexOrThrow(StoreTable.ID_PLACE)));
    }

    public Store(String name, Float estimatedServiceTime, Place place) {
        this.name = name;
        this.estimatedServiceTime = estimatedServiceTime;
        this.place = place;
    }

    public Store(Context context, com.cakebackoffice.clientsoap.contract.Store store) {
        this.remoteID = Long.valueOf(store.id);
        this.name = store.name;
        this.place = new Place(store.adress);
        this.place.saveToDatabase(context);
    }

    public Long getRemoteID() {
        return remoteID;
    }

    public void setRemoteID(Long remoteID) {
        this.remoteID = remoteID;
    }

    public Long getIdDB() {
        return idDB;
    }

    public void setIdDB(Long idDB) {
        this.idDB = idDB;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Place getPlace() {
        return place;
    }

    public void setPlace(Place place) {
        this.place = place;
    }

    public float getEstimatedServiceTime() {
        return estimatedServiceTime;
    }

    public void setEstimatedServiceTime(float estimatedServiceTime) {
        this.estimatedServiceTime = estimatedServiceTime;
    }

//    public Dictionary<ProductType, LocalPriceHT> getAvailableTypeProducts() {
//        return availableTypeProducts;
//    }
//
//    public void setAvailableTypeProducts(Dictionary<ProductType, LocalPriceHT> availableTypeProducts) {
//        this.availableTypeProducts = availableTypeProducts;
//    }
//
//    public Dictionary<MenuType, LocalPriceHT> getAvailableMenuType() {
//        return availableMenuType;
//    }
//
//    public void setAvailableMenuType(Dictionary<MenuType, LocalPriceHT> availableMenuType) {
//        this.availableMenuType = availableMenuType;
//    }

    @Override
    public ContentValues getValues() {
        ContentValues values = new ContentValues();
        values.put(StoreTable.REMOTE_ID, this.getRemoteID());
        values.put(StoreTable.NAME, this.getName());
        values.put(StoreTable.ESTIMATEDSERVICETIME, this.getEstimatedServiceTime());
        values.put(StoreTable.ID_PLACE, this.getPlace() != null ? this.getPlace().getIdDB() : null);

        return values;
    }


    @Override
    public void saveToDatabase(Context context) {
        //Protection anti-doublons
        Store bis = null;
        if (this.getIdDB() == null && this.getRemoteID() != null && (bis = Store.getOneFromRemoteID(context, this.getRemoteID())) != null) {
            this.setIdDB(bis.getIdDB());
        }

        DataSource dataSource = new DataSource(context);
        dataSource.open();
        if (this.getIdDB() == null) {
            dataSource.insert(this, StoreTable.getInstance());
        } else {
            dataSource.update(this, StoreTable.getInstance());
        }
        {
            //Aucune Liste !   C'est trop bien !
        }
        dataSource.close();
    }

    @Override
    public void removeFromDatabase(Context context) {
        if (this.getIdDB() != null) {
            DataSource dataSource = new DataSource(context);
            dataSource.open();
            dataSource.delete(this, StoreTable.getInstance());
            dataSource.close();
        }
    }

    public static Store getOne(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOne(idDB, StoreTable.getInstance());
        cursor.moveToFirst();
        Store item = null;
        while (!cursor.isAfterLast()) {
            item = new Store(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static Store getOneFromRemoteID(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOneFromRemoteID(idDB, StoreTable.getInstance());
        cursor.moveToFirst();
        Store item = null;
        while (!cursor.isAfterLast()) {
            item = new Store(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static List<Store> getAll(Context context) {
        List<Store> result = new ArrayList<Store>();
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchAll(StoreTable.getInstance());
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            result.add(new Store(dataSource, cursor));
            cursor.moveToNext();
        }
        dataSource.close();
        return result;
    }

    public static Store getOne(DataSource dataSource, long idDB) {
        Cursor cursor = dataSource.fetchOne(idDB, StoreTable.getInstance());
        cursor.moveToFirst();
        Store item = null;
        while (!cursor.isAfterLast()) {
            item = new Store(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        return item;
    }

    public static Store getLocalStore(Context context) {
        SharedPreferences settings = context.getSharedPreferences("Local", 0);
        return Store.getOneFromRemoteID(context, settings.getLong("idLocalStore", 1l));
    }

    public static void setLocalStore(Context context, Store ourInstance) {
        SharedPreferences settings = context.getSharedPreferences("Local", 0);
        settings.edit().putLong("idLocalStore", ourInstance.getRemoteID()).commit();
    }

}
